package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.License;
import com.community.service.LicenseService;
import com.community.util.vehicle.LicensePlate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (License)表控制层
 *
 * @author makejava
 * @since 2021-10-14 14:21:18
 */
@Controller
@RequestMapping("license")
public class LicenseController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private LicenseService licenseService;
    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @RequestMapping("camera2")
    public String tocamera2() {
        return "camera2";
    }

    @RequestMapping("camera3")
    public String tocamera3() {
        return "camera3";
    }

    @RequestMapping("exception")
    public String exception() {
        return "exception";
    }

    @RequestMapping("car_inandout")
    public String tocar_inandout() {
        return "car_inandout";
    }

    @PutMapping("updatebylicenseplate")
    @ResponseBody
    public Object updatebylicenseplate(String licensePlate) {
        License license = licenseService.getOne(new QueryWrapper<License>().eq("license_plate", licensePlate));
        if (license.getStatuc() == 1) {
            license.setStatuc(2);
        } else if (license.getStatuc() == 2) {
            license.setStatuc(1);
        }
        return success(this.licenseService.updateById(license));
    }

    @PostMapping("getlicense")
    @ResponseBody
    public Object getlicense(@RequestParam String imgData) throws ServletException, IOException {
        BASE64Decoder decoder = new BASE64Decoder();//base64转为二进制
        byte[] date_blob = decoder.decodeBuffer(imgData);
        for (int i = 0; i < date_blob.length; ++i) {
            if (date_blob[i] < 0) {
                date_blob[i] += 256;
            }
        }
        System.out.println("二进制：" + date_blob);
        System.out.println(date_blob.length);
//        将二进制转为本地图片
        try {
            File file = new File(uploadFolder + "/MYTESTPIC.png");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(date_blob);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        LicensePlate licensePlate = new LicensePlate();
        String result = licensePlate.licensePlate(uploadFolder + "/MYTESTPIC.png");
        return result;
    }

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param license 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
    public R selectAll(Page<License> page, License license) {
        page.setSize(4);
        QueryWrapper<License> wrapper = new QueryWrapper<>();
        wrapper.like(license.getLicensePlate() != null, "license_plate", license.getLicensePlate());
        wrapper.between(license.getBeginDate() != null && license.getOverDate() != null,
                "modify_date", license.getBeginDate(), license.getOverDate());
        wrapper.orderByDesc("modify_date");
        return success(this.licenseService.page(page, wrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ResponseBody
    public R selectOne(@PathVariable Serializable id) {
        return success(this.licenseService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param license 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public R insert(License license) {
        license.setModifyDate(new Date());
        return success(this.licenseService.save(license));
    }

    /**
     * 修改数据
     *
     * @param license 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    @ResponseBody
    public R update(License license) {
        return success(this.licenseService.updateById(license));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.licenseService.removeByIds(idList));
    }
}

